package config

import (
	"fmt"
	"github.com/micro/go-micro/v2/config"
	"strings"
)

type Host struct {
	Address  string `json:"address"`
	Port     int    `json:"port"`
	Url      string `json:"url"`
	UserName string `json:"user_name"`
	Password string `json:"password"`
}

type Config struct {
	Hosts map[string]Host `json:"hosts"`
}

func init() {
	err := config.LoadFile("config.json")
	if err != nil {
		fmt.Println(err)
	}
	initServer()
}

func (h *Host) GetHost(path string, name string) {
	config.Get(path, name).Scan(h)
	portStr := fmt.Sprintf("%d", h.Port)
	url := "{address}:{port}"
	url = strings.Replace(url, "{address}", h.Address, -1)
	url = strings.Replace(url, "{port}", portStr, -1)
	h.Url = url
	fmt.Println(url)
}

/*数据库mysql*/
type DBConfig struct {
	Dialect      string
	Database     string
	User         string
	Password     string
	Charset      string
	Host         string
	Port         int
	SQLLog       bool
	URL          string
	Prefix       string
	MaxIdleConns int
	MaxOpenConns int
}

func (db *DBConfig) GetDbConfig(database string) {
	err := config.Get("hosts", database).Scan(db)
	if err != nil {
		fmt.Println(err)
	}
	portStr := fmt.Sprintf("%d", db.Port)
	url := "{user}:{password}@tcp({host}:{port})/{database}?charset={charset}&parseTime=true"
	url = strings.Replace(url, "{database}", db.Database, -1)
	url = strings.Replace(url, "{user}", db.User, -1)
	url = strings.Replace(url, "{password}", db.Password, -1)
	url = strings.Replace(url, "{host}", db.Host, -1)
	url = strings.Replace(url, "{port}", portStr, -1)
	url = strings.Replace(url, "{charset}", db.Charset, -1)
	db.URL = url
	fmt.Println(url)
}

type serverConfig struct {
	Debug               bool   `json:"debug"`
	ImgPath             string `json:"img_path"`
	UploadImgDir        string `json:"upload_img_dir"`
	Port                int    `json:"port"`
	SessionID           string `json:"session_id"`
	MaxOrder            int    `json:"max_order"`
	MinOrder            int    `json:"min_order"`
	PageSize            int    `json:"page_size"`
	MaxPageSize         int    `json:"max_page_size"`
	MinPageSize         int    `json:"min_page_size"`
	MaxNameLen          int    `json:"max_name_len"`
	MaxRemarkLen        int    `json:"max_remark_len"`
	MaxContentLen       int    `json:"max_content_len"`
	MaxProductCateCount int    `json:"max_product_cate_count"`
	MaxProductImgCount  int    `json:"max_product_img_count"`
	AccessTokenExpire   int    `json:"access_token_expire"`
	LogPath             string `json:"log_path"`
	LogLevel            string `json:"log_level"`
	ConcurrencyQueue    int    `json:"concurrency_queue"`
	Env                 string `json:"env"`
}

// ServerConfig 服务器相关配置
var ServerConfig serverConfig

type settingConfig struct {
	AllowIps      string
	AppName       string
	ApiKey        string
	ApiSecret     string
	ApiSignEnable bool

	EnableTLS bool
	CAFile    string
	CertFile  string
	KeyFile   string
	// Installed 应用是否已安装
	Installed bool // 应用是否安装过

	ConcurrencyQueue int
	AuthSecret       string
}

var Setting settingConfig

func initServer() {
	config.Get("hosts", "go").Scan(&ServerConfig)
	config.Get("hosts", "setting").Scan(&Setting)
}
